package com.micangcaihang.micang.mvp.task.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.micangcaihang.common.base.fragment.BaseListFragment;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.adapter.DTabBackTaskAdapter;
import com.micangcaihang.micang.databinding.FragmentTabBackTaskDBinding;
import com.micangcaihang.micang.entity.TaskListEntity;
import com.micangcaihang.micang.mvp.task.contract.DTabBackTaskContract;
import com.micangcaihang.micang.mvp.task.presenter.DTabBackTaskPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/7/30
 *     update : 2017/7/30
 *     desc   : 调评退回任务fragment
 *     version: 1.0
 * </pre>
 */
public class DTabBackTaskFragment extends BaseListFragment<DTabBackTaskPresenter,
        DTabBackTaskAdapter, FragmentTabBackTaskDBinding> implements DTabBackTaskContract.View {
    private static final int DELETE_TASK_ITEM = 0X11;
    private int currentItemPos;

    public static DTabBackTaskFragment newInstance() {
        Bundle args = new Bundle();
        DTabBackTaskFragment fragment = new DTabBackTaskFragment();
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_back_task_d;
    }

    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        mViewBinding.swipeLayout.setColorSchemeResources(R.color.colorPrimary);
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                currentItemPos = position;
                Bundle bundle = new Bundle();
                bundle.putInt("type", mAdapter.getData().get(position).getItemType());
                bundle.putString("id", mAdapter.getData().get(position).getBusiness_id());
                bundle.putString("reback", mAdapter.getData().get(position).getReback_id());
                bundle.putInt("status", mAdapter.getData().get(position).getStatus());
                startActivityForResult(DTaskDetailActivity.class, DELETE_TASK_ITEM, bundle);
            }
        });
    }

    /**
     * recycleView的设置
     */
    @Override
    protected void initAdapter() {
        mAdapter = new DTabBackTaskAdapter(new ArrayList<>());
    }

    /**
     * 初始化各种状态处理
     * 在这个方法里处理的是recyclerview的所有的初始化，
     * 包括对他的展示形式，是list或grid或瀑布流
     */
    @Override
    protected void initRecyclerViewSetting() {

    }

    /**
     * 返回退回任务列表任务
     *
     * @param taskLists 退回任务列表
     */
    @Override
    public void returnTaskList(List<TaskListEntity> taskLists) {
        mAdapter.setNewData(taskLists);
        mProgressLinearLayout.showContent();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case DELETE_TASK_ITEM://退回任务或者终止任务成功
                    mAdapter.remove(currentItemPos);
                    if (!(mAdapter.getData().size() > 0)) {
                        showEmptyLayout();
                    }
                    break;
            }
        }
    }
}
